﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Commonlib;
namespace AlbonWeb.Manager.AccountManager
{
    public partial class UserAdd : System.Web.UI.Page
    {
        private readonly BLL.UserBLL userbll = new BLL.UserBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["Back"] = Request.UrlReferrer.ToString();
                //绑定角色信息
                string cmdstr = "select * from RoleInfo";
                SqlHelper.BindDropDownList(ddlRole, "roleName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
                ddlRole.Items.Insert(0, new ListItem("--select role--", "0"));
                //绑定付款方式选项
                cmdstr = "select * from PayMethod";
                SqlHelper.BindDropDownList(ddlPay, "methodName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
                ddlPay.Items.Insert(0, new ListItem("--pay method--", "0"));
                //绑定发货方式选项
                cmdstr = "select * from ShipMethod";
                SqlHelper.BindDropDownList(ddlShip, "methodName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
                ddlShip.Items.Insert(0, new ListItem("--ship method--", "0"));
                if (Request["userid"] != null)
                {
                    //加载要修改的数据
                    initUser(Request["userid"].ToString());
                }
            }
        }

        //加载用户数据
        private void initUser(string userid)
        {
            DataTable dt = userbll.getUserByID(userid);
            ddlRole.SelectedValue = dt.Rows[0]["roleID"].ToString();
            userName.Text = dt.Rows[0]["userName"].ToString();
            userName.Enabled = false;
            password.Text = dt.Rows[0]["userPsw"].ToString();
            ddlPay.SelectedValue = dt.Rows[0]["payMethod"].ToString();
            ddlShip.SelectedValue = dt.Rows[0]["shipMethod"].ToString();
            ddlMaxUser.SelectedValue = dt.Rows[0]["maxUser"].ToString();
            credible.Checked = bool.Parse(dt.Rows[0]["credible"].ToString());
            active.Checked = bool.Parse(dt.Rows[0]["active"].ToString());
            remark.InnerText = dt.Rows[0]["remark"].ToString();
        }
        //添加或修改记录
        protected void userAdd_btnNext_Click(object sender, EventArgs e)
        {
            Albon.Model.UserAccount user = new Albon.Model.UserAccount();
            user.ID = 0;
            if (Request["userid"] != null)
            {
                user.ID = int.Parse(Request["userid"].ToString());
            }
            user.roleID = int.Parse(ddlRole.SelectedValue);
            user.userName = userName.Text.Trim();
            user.userPsw = password.Text;
            user.payMethod = int.Parse(ddlPay.SelectedValue);
            user.shipMethod = int.Parse(ddlShip.SelectedValue);
            user.maxUser = int.Parse(ddlMaxUser.SelectedValue);
            user.credible = credible.Checked;
            user.active = active.Checked;
            user.remark = remark.InnerText;
            userbll.alterUser(user);
            Response.Redirect(ViewState["Back"].ToString());
        }

    }
}